package com.zs.backend.modules.orderLog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zs.backend.common.UserHolder;
import com.zs.backend.common.enumeration.OrderLogType;
import com.zs.backend.common.utils.BeanUtil;
import com.zs.backend.modules.certificate.model.UserCertificate;
import com.zs.backend.modules.orderLog.dto.OrderLogDTO;
import com.zs.backend.modules.orderLog.dto.OrderLogResponse;
import com.zs.backend.modules.orderLog.model.OrderLog;
import com.zs.backend.modules.orderLog.mapper.OrderLogMapper;
import com.zs.backend.modules.orderLog.service.OrderLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单记录表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2024-03-25
 */
@Service
public class OrderLogServiceImpl extends ServiceImpl<OrderLogMapper, OrderLog> implements OrderLogService {

    @Override
    public OrderLogResponse queryLogByType(Integer type, Long orderId) {
        if(type == null || orderId == null){
            return new OrderLogResponse();
        }
        QueryWrapper<OrderLog> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<OrderLog> lambda = wrapper.lambda();
        lambda.eq(OrderLog::getOperatitonType, type);
        lambda.eq(OrderLog::getOrderId, orderId);
        List<OrderLog> list = this.list(lambda);
        List<OrderLogDTO> orderLogDTOS = BeanUtil.beanCopyPropertiesForList(list, OrderLogDTO.class);
        OrderLogResponse orderLogResponse = new OrderLogResponse();
        orderLogResponse.setType(type);
        orderLogResponse.setOrderLogs(orderLogDTOS);
        return orderLogResponse;
    }

    @Override
    public Boolean saveOrderLog(OrderLogDTO orderLogDTO) {
        orderLogDTO.setOperatitonUname(UserHolder.getCurrentAdmin().getUsername());
        orderLogDTO.setCreateTime(new Date());
        orderLogDTO.setOperatitonUid(UserHolder.getCurrentAdmin().getId());
        OrderLog orderLog = BeanUtil.beanCopyProperties(orderLogDTO, OrderLog.class);
        return save(orderLog);
    }
}
